﻿var OffersNS = (function () {
    var TransportType = Object.freeze({
        "air": "Air",
        "land": "Land",
        "combined": "Combined",
        "own": "Own Transportation",
    });

    var Offer = Class.create({
        initialize: function (params) {
            this.id = params.id;
            this.title = params.title;
            this.startDate = params.startDate;
            this.endDate = params.endDate;
            this.price = params.price;
            this.thumbnailUrl = params.thumbnailUrl;
            this.imageUrl = params.imageUrl;
            this.description = params.description;
            this.active = params.active | false;
        },

        activate: function () {
            this.active = true;
        },

        deactivate: function () {
            this.active = false;
        },

        render: function () {
            var rendered = jQuery('<div class="offer cruise" id="' + this.id + '">' +
                '<div class="thumbnail"><img src="' + this.thumbnailUrl + '" alt="cruise-offer"/></div>' +
                '<div class="offer-title"><strong>' + this.title + '</strong></div>' +
                '<div class="offer-date">(' + this.startDate + ' - ' + this.endDate + ')</div>' +
                '<div class="offer-price">Price: ' + this.price / 2 + '€ / ' + this.price + 'lv.</div>' +
                '<div class="offer-description">' + this.description + '</div>' +
                '</div>');

            rendered.on('click', onClick);
            return rendered;
        }
    });

    // Private functions
    function enlargeOffer(currentOffer) {
        var isCurrentOfferEnlarged = currentOffer.hasClass('enlarged-offer');
        jQuery('.enlarged-offer').removeClass('enlarged-offer');
        var img = jQuery('div.thumbnail img', currentOffer);

        if (isCurrentOfferEnlarged) {
            if (currentOffer.hasClass('enlarged-by-slider')) {
                currentOffer.remove();
                return;
            }
            img.attr("src", self.thumbnailUrl);
        }
        else {
            currentOffer.addClass('enlarged-offer');
            img.attr("src", self.imageUrl);
        }
    }

    function onClick() {
        var currentOffer = jQuery(this);

        currentOffer.hide(1500, function () {
            enlargeOffer(currentOffer);
        });
        currentOffer.fadeIn(1500);
    }

    var PackageHoliday = Class.create(Offer, {
        initialize: function ($super, params) {
            $super(params);
            this.transportType = params.transportType;
        },

        render: function ($super) {
            var rendered = jQuery($super());
            rendered.append('<div class="transport-type">Transport type - ' + this.transportType + ' </div>');
            return rendered;
        }
    });

    var Excursion = Class.create(Offer, {
        initialize: function ($super, params) {
            $super(params);
            this.transportType = params.transportType;
            this.itinerary = params.itinerary;
        },

        render: function ($super) {
            var rendered = jQuery($super());
            rendered.append('<div class="transport-type">Transport type - ' + this.transportType + ' </div>');
            //rendered.append('<div class="offer-itinerary">' + this.itinerary.render().html() + '</div>');
            return rendered;
        },

        renderItinerary: function () {
            var rendered = $('<ol></ol>');
            for (var i = 0; i < this.itinerary.days.length; i++) {
                remdered.append('<li>' + this.itinerary.days[i].day.city + '": "' +
                    this.itinerary.days[i].day.description + '</li>');
            }

            return rendered;
        }

    });

    var Cruise = Class.create(Offer, {
        initialize: function ($super, params) {
            $super(params);
            this.waterbody = params.waterbody;
        },

        render: function ($super) {
            var rendered = jQuery($super());
            rendered.append('<div class="offer-waterbody"><strong>' + this.waterbody + '</strong></div>');
            return rendered;
        },
    });

    var Itinerary = Class.create({
        initialize: function () {
            this.days = [];
        },

        add: function (city, description) {
            this.days.push({ "city": city, "description": description });
        },

        render: function () {
            var rendered = $('<dl class="itinerary"></dl>');
            for (var i = 0; i < this.days.length; i++) {
                rendered.append('<dt>Day ' + (i + 1) + ": " + this.days[i].city + '</dt>');
                rendered.append('<dd>' + this.days[i].description + '</dd>');
            }

            return rendered.appendTo('<div></div>');
        }
    });

    return {
        TransportType: TransportType,
        PackageHoliday: PackageHoliday,
        Excursion: Excursion,
        Cruise: Cruise,
        Itinerary: Itinerary,
    };
}());